//
//  LCPointView.m
//  HQBProject
//
//  Created by taolei-mac on 2017/4/19.
//  Copyright © 2017年 com.zhejiangchain.ios. All rights reserved.
//

#define DEGREES_DefaultStart(degrees)  ((M_PI * (degrees+270))/ 180) //默认270度为开始的位置

#import "LCPointView.h"

@interface LCPointView ()<CAAnimationDelegate>
@property (nonatomic, strong) CAShapeLayer *pointerLayer;
@property (nonatomic, assign) CGFloat startAngle;
@property (nonatomic, strong) CAShapeLayer *edgeLayer;
@end

@implementation LCPointView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
    
        [self addPointView];
    }
    return self;
}

- (void)addPointView
{
    CAShapeLayer *pointerLayer = [CAShapeLayer layer];
    pointerLayer.frame = CGRectMake((self.width - 10) * 0.5, 5, 5, 5);
    pointerLayer.backgroundColor = LCColor(255, 255, 255).CGColor;
    pointerLayer.cornerRadius = CGRectGetWidth(pointerLayer.frame)/2;
    [self.layer addSublayer:pointerLayer];
    self.pointerLayer = pointerLayer;
}

- (void)updatePointView:(NSInteger)percent withAnimationTime:(CGFloat)animationTime
{
    CAKeyframeAnimation *pointViewAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pointViewAnimation.duration = animationTime;
    pointViewAnimation.removedOnCompletion = NO;
    pointViewAnimation.fillMode = kCAFillModeForwards;
    // 使动画均匀进行
    pointViewAnimation.calculationMode = kCAAnimationPaced;
    
    // 创建一条直线
    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat origin_x = self.width * 0.5;
    CGFloat origin_y = self.height * 0.5;
    CGFloat radiusX = (self.height - 30) * 0.5;
    CGPathAddArc(path, nil, origin_x, origin_y, radiusX, -M_PI * 0.5, 0, 0);
    pointViewAnimation.path = path;
    [self.pointerLayer addAnimation:pointViewAnimation forKey:nil];
}

//- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
//    CGFloat angle = [[anim valueForKey:@"angle"] floatValue];
//    self.startAngle = angle;
//    self.layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);
//}


@end
